package cn.enilu.flash.service.dashboard;

import cn.enilu.flash.bean.entity.shop.Order;
import cn.enilu.flash.bean.enumeration.shop.OrderEnum;
import cn.enilu.flash.bean.vo.query.SearchFilter;
import cn.enilu.flash.service.shop.CartService;
import cn.enilu.flash.service.shop.OrderService;
import cn.enilu.flash.service.shop.ShopUserService;
import cn.enilu.flash.utils.Maps;
import cn.enilu.flash.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author ：enilu
 * @date ：Created in 1/7/2020 2:25 PM
 */
@Service
public class DashboardService {
    @Autowired
    private OrderService orderService;
    @Autowired
    private ShopUserService shopUserService;
    @Autowired
    private CartService cartService;

    public Map getDashboardData() {
        long orderCount = orderService.count();
        long userCount = shopUserService.count();
        long cartCount = cartService.count();
        List<Order> orderList = orderService.queryAll();
        Long unPay = orderList.stream().filter(l -> {
            if (l.getStatus().equals(OrderEnum.OrderStatusEnum.UN_PAY.getId())) {
                return true;
            } else {
                return false;
            }
        }).count();
        BigDecimal unPayPrice = orderList.stream().filter(l -> {
            if (l.getStatus().equals(OrderEnum.OrderStatusEnum.UN_PAY.getId())) {
                return true;
            } else {
                return false;
            }
        }).map(Order::getRealPrice).reduce(BigDecimal.ZERO,BigDecimal::add);
        Long unSend = orderList.stream().filter(l -> {
            if (l.getStatus().equals(OrderEnum.OrderStatusEnum.UN_SEND.getId())) {
                return true;
            } else {
                return false;
            }
        }).count();
        BigDecimal unSendPrice = orderList.stream().filter(l -> {
            if (l.getStatus().equals(OrderEnum.OrderStatusEnum.UN_SEND.getId())) {
                return true;
            } else {
                return false;
            }
        }).map(Order::getRealPrice).reduce(BigDecimal.ZERO,BigDecimal::add);

        Long sended = orderList.stream().filter(l -> {
            if (l.getStatus().equals(OrderEnum.OrderStatusEnum.SENDED.getId())) {
                return true;
            } else {
                return false;
            }
        }).count();
        BigDecimal sendedPrice = orderList.stream().filter(l -> {
            if (l.getStatus().equals(OrderEnum.OrderStatusEnum.SENDED.getId())) {
                return true;
            } else {
                return false;
            }
        }).map(Order::getRealPrice).reduce(BigDecimal.ZERO,BigDecimal::add);

        Long finished = orderList.stream().filter(l -> {
            if (l.getStatus().equals(OrderEnum.OrderStatusEnum.FINISHED.getId())) {
                return true;
            } else {
                return false;
            }
        }).count();
        BigDecimal finishedPrice = orderList.stream().filter(l -> {
            if (l.getStatus().equals(OrderEnum.OrderStatusEnum.FINISHED.getId())) {
                return true;
            } else {
                return false;
            }
        }).map(Order::getRealPrice).reduce(BigDecimal.ZERO,BigDecimal::add);

        Long cancel = orderList.stream().filter(l -> {
            if (l.getStatus().equals(OrderEnum.OrderStatusEnum.CANCEL.getId())) {
                return true;
            } else {
                return false;
            }
        }).count();
        BigDecimal cancelPrice = orderList.stream().filter(l -> {
            if (l.getStatus().equals(OrderEnum.OrderStatusEnum.CANCEL.getId())) {
                return true;
            } else {
                return false;
            }
        }).map(Order::getRealPrice).reduce(BigDecimal.ZERO,BigDecimal::add);

        Long refundIng = orderList.stream().filter(l -> {
            if (l.getStatus().equals(OrderEnum.OrderStatusEnum.REFUND_ING.getId())) {
                return true;
            } else {
                return false;
            }
        }).count();
        BigDecimal refundIngPrice = orderList.stream().filter(l -> {
            if (l.getStatus().equals(OrderEnum.OrderStatusEnum.REFUND_ING.getId())) {
                return true;
            } else {
                return false;
            }
        }).map(Order::getRealPrice).reduce(BigDecimal.ZERO,BigDecimal::add);

        Long refund = orderList.stream().filter(l -> {
            if (l.getStatus().equals(OrderEnum.OrderStatusEnum.REFUND.getId())) {
                return true;
            } else {
                return false;
            }
        }).count();
        BigDecimal refundPrice = orderList.stream().filter(l -> {
            if (l.getStatus().equals(OrderEnum.OrderStatusEnum.REFUND.getId())) {
                return true;
            } else {
                return false;
            }
        }).map(Order::getRealPrice).reduce(BigDecimal.ZERO,BigDecimal::add);
        Map result = Maps.newHashMap(
                "orderCount", orderCount,
                "userCount", userCount,
                "cartCount", cartCount,
                "unPay",unPay,
                "unPayPrice",unPayPrice,
                "unSend",unSend,
                "unSendPrice",unSendPrice,
                "sended",sended,
                "sendedPrice",sendedPrice,
                "finished",finished,
                "finishedPrice",finishedPrice,
                "cancel",cancel,
                "cancelPrice",cancelPrice,
                "refunding",refundIng,
                "refundingPrice",refundIngPrice,
                "refund",refund,
                "refundPrice",refundPrice
        );
        return result;
    }
}
